﻿using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using TouchSocket.Core.ByteManager;
using UnityEngine;

public class FHClientController : MonoBehaviour
{
    public FHTcpClient fhTcpClient;
    //public LitVCR litVCR;
    private int receiveDataType;
    private int sendDataType;

    public bool OffLightWhenPlayMovie;
    bool exit;
    // Start is called before the first frame update
    void Start()
    {

        fhTcpClient = new FHTcpClient();
        string ipPort = File.ReadAllText(Path.Combine(Application.streamingAssetsPath, "ip_port.txt"));
        fhTcpClient.InitConfig(ipPort);
        Loom.RunAsync(() =>
        {
            exit = false;
            while (!exit)
            {
                if (!fhTcpClient.IsOnline())
                {
                    fhTcpClient.StartConnect();
                }
            }
        });
        fhTcpClient.FHTcpClientReceive = ReceiveData;
        string receiveDataTypestr = File.ReadAllText(Path.Combine(Application.streamingAssetsPath, "receiveDataType.config"));
        receiveDataType = Convert.ToInt32(receiveDataTypestr);
        string sendDataTypestr = File.ReadAllText(Path.Combine(Application.streamingAssetsPath, "sendDataType.config"));
        sendDataType = Convert.ToInt32(sendDataTypestr);

        //litVCR.StartPlaying += () =>
        //{
        //    LightOff();

        //};
        //litVCR.FinishPlaying += () =>
        //{
        //    LightOn();

        //};
    }

    private void LightOn()
    {
        if (OffLightWhenPlayMovie)
        {
            fhTcpClient.Send(DataTypeEnum.ZheZhao, OrderTypeEnum.ZheZhao, false);
            ShaPanProtocol.ins.OpenShapanLight();
        }
    }

    private void LightOff()
    {
        if (OffLightWhenPlayMovie)
        {
            fhTcpClient.Send(DataTypeEnum.ZheZhao, OrderTypeEnum.ZheZhao, true);
            ShaPanProtocol.ins.CloseShapanLight();
        }
    }

    private void ReceiveData(DTOInfo info)
    {
        if (info.DataType == (int)DataTypeEnum.ShutDown)
        {
            if (info.OrderType == (int)OrderTypeEnum.ShutDown)
            {
                string toolpath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "tool", "PowerController.exe");
                if (File.Exists(toolpath))
                {
                    System.Diagnostics.Process.Start(toolpath, "0");
                }
            }
        }

        if (info.DataType == receiveDataType)
        {
            switch ((OrderTypeEnum)info.OrderType)
            {
                case OrderTypeEnum.GetFileList:
                    //fhTcpClient.Send((DataTypeEnum)sendDataType, OrderTypeEnum.GetFileList, litVCR.GetFileNames());
                    break;
                case OrderTypeEnum.GetMovAllSecond:
                    //fhTcpClient.Send((DataTypeEnum)sendDataType, OrderTypeEnum.GetMovAllSecond, litVCR.GetTotalSec());
                    break;
                case OrderTypeEnum.GetMovSeek:
                    //fhTcpClient.Send((DataTypeEnum)sendDataType, OrderTypeEnum.GetMovSeek, litVCR.GetVideoSeek());
                    break;
                case OrderTypeEnum.GetCurMovieName:
                    //fhTcpClient.Send((DataTypeEnum)sendDataType, OrderTypeEnum.GetCurMovieName, litVCR.GetPlayingName());
                    break;
                case OrderTypeEnum.GetVolumn:
                    //fhTcpClient.Send((DataTypeEnum)sendDataType, OrderTypeEnum.GetVolumn, litVCR.GetVolumn());
                    break;
                case OrderTypeEnum.SetVolumn:
                    float volumn = JsonConvert.DeserializeObject<float>(Encoding.UTF8.GetString(info.Body));
                    //litVCR.SetVolumn(volumn);
                    break;
                case OrderTypeEnum.SetMovSeek:
                    float seek = JsonConvert.DeserializeObject<float>(Encoding.UTF8.GetString(info.Body));
                    //litVCR.SetVideoSeekSlider(seek);
                    break;
                case OrderTypeEnum.PlayNext:
                    //litVCR.PlayNext();
                    break;
                case OrderTypeEnum.PlayPrev:
                    //litVCR.PlayPrev();
                    break;
                case OrderTypeEnum.SetPlayMovie:
                    string movname = JsonConvert.DeserializeObject<string>(Encoding.UTF8.GetString(info.Body));
                    print("收到指定播放视频");
                    //litVCR.OpenVideoFileByFileName(movname);
                    break;
                case OrderTypeEnum.PlayMovie:
                    //litVCR.OnPlayButton();
                    LightOff();
                    break;
                case OrderTypeEnum.StopMovie:
                    //litVCR.OnRewindButton();
                    //litVCR.SetDisplayEnable(false);
                    LightOn();
                    break;
                case OrderTypeEnum.PauseMovie:
                    //litVCR.OnPauseButton();
                    //LightOn();
                    break;

            }
        }
    }

    /// <summary>
    /// 转发对象
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="dataTypeEnum"></param>
    /// <param name="orderTypeEnum"></param>
    /// <param name="data"></param>
    public void Send<T>(DataTypeEnum dataTypeEnum, OrderTypeEnum orderTypeEnum, T data)
    {
        fhTcpClient.Send(dataTypeEnum, orderTypeEnum, data);
    }
    public void SendHex(DataTypeEnum dataTypeEnum, OrderTypeEnum orderTypeEnum, string obj)
    {
        fhTcpClient.SendHexStr(dataTypeEnum, orderTypeEnum, obj);
    }

    private void OnDisable()
    {
        if (fhTcpClient != null)
        {
            fhTcpClient.Close();
        }
        exit = true;
    }
}
